//  程序在运行过程中的共享域
//  Singleton.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-7.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "Singleton.h"
#import "DataBaseAccess.h"
#import "NSString+Additions.h"
#import "Common.h"
@interface Singleton(Private)
-(void) initPluMenu:(NSString *)pmCodes;//注意传入的参数
-(void) initPluSort:(NSString *)pmCode;
@end


@implementation Singleton
@synthesize musicPlayer;
@synthesize fullScreenType;
@synthesize hotelName;
@synthesize ipadUdid;
@synthesize downloadUrl;
@synthesize supportLangs;
@synthesize langsArray;
@synthesize isUpdate;
@synthesize isOEMDisplay;
@synthesize isCommendView;
@synthesize isSortedByMenu;
@synthesize language;
@synthesize translate;
@synthesize isAnimating;
@synthesize isViewRemoveFromLeft,isPopOutFullScreen;
@synthesize popController;
@synthesize currentOrderMenu;
@synthesize pluBooks;
@synthesize bookIndex;
@synthesize currentPmCodes;
@synthesize pluMenus;
@synthesize menuIndex;
@synthesize pluSorts;
@synthesize sortIndex;
@synthesize searchPlus;
@synthesize searchIndex;
@synthesize commendPluPlus;
@synthesize commendIndex;
@synthesize orderingPluPlus;
@synthesize pluIndex;
@synthesize myOrderMenus;
@synthesize myOrderMenuIndex;
@synthesize myOrderDishes;
@synthesize myOrderPluPkgs;
@synthesize myOrderPluIndex;
@synthesize currentMyOrderDish;
@synthesize myOrderSortedType;
@synthesize myOrderSortedDishes;
@synthesize remoteMenus;
@synthesize remoteMenuIndex;
@synthesize remoteSortedMenus;
@synthesize remoteSortedDishes;

@synthesize pluTables;
@synthesize pluEmpnos;
@synthesize pluSpecialSorts;
@synthesize specialSortIndex;
@synthesize pluSpecialCodes;
@synthesize specialCodeIndex;
@synthesize otherImages;
@synthesize otherImageIndex;

//singleton设计模式
static Singleton *instance;
+(Singleton *)sharedSingleton {
	@synchronized(self)
	{
		if (instance == nil) {
            instance = [[Singleton alloc] init];
		}
		return instance;
	}
}
-(void) setIsAnimating:(BOOL)isAnimating1{
    isAnimating = isAnimating1;
    if (isAnimating1 == NO) {
        MyNSLog(@"isAnimating1 == NO");
    }
}
- (void)playMusic:(NSString *) musicPath{    
	if (self.musicPlayer != nil) {
        if (self.musicPlayer.isPlaying) {
            [self.musicPlayer stop];
        }
	}    
    AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
    self.musicPlayer = soundPlayer;
    [soundPlayer release];
    [self.musicPlayer prepareToPlay];
	self.musicPlayer.numberOfLoops = 0;
	self.musicPlayer.volume = 1;
    self.musicPlayer.delegate = self;
    self.musicPlayer.meteringEnabled = YES;
	
    [self.musicPlayer play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {//铃声播放完成后，自动调用的委托方法
	[self stopMusic];
}
- (void)stopMusic{
    if (self.musicPlayer && self.musicPlayer.isPlaying) {
        [self.musicPlayer stop];
	}
}

//初始化桌号
-(void) initPluTable{
    if (self.pluTables != nil && [self.pluTables count]>0) {
        [self.pluTables removeAllObjects];
    }
    self.pluTables = [DataBaseAccess selectPluTable:@"select * from PluTable order by ptTableNo asc"];
}
//初始化工号
-(void) initPluEmpno{
    if (self.pluEmpnos != nil && [self.pluEmpnos count]>0) {
        [self.pluEmpnos removeAllObjects];
    }
    self.pluEmpnos = [DataBaseAccess selectPluEmpno:@"select * from PluEmpno order by peEmpno asc"];
}
//初始化烧法类别
-(void) initSpecialSorts{
    if (self.pluSpecialSorts != nil && [self.pluSpecialSorts count]>0) {
        [self.pluSpecialSorts removeAllObjects];
    }
    self.pluSpecialSorts = [DataBaseAccess selectPluSpecialSort:@"select * from PluSpecialSort order by cast(pssCode as int) asc"];
    [self initSpecialCodes:0];
}
//初始化烧法明细
-(void) initSpecialCodes:(NSInteger)index{
    if (self.pluSpecialCodes != nil && [self.pluSpecialCodes count]>0) {
        [self.pluSpecialCodes removeAllObjects];
    }
    self.specialSortIndex = index;
    if (0 <= index && index < [self.pluSpecialSorts count]) {
        PluSpecialSort * temp = (PluSpecialSort *)[self.pluSpecialSorts objectAtIndex:index];
        self.pluSpecialCodes = [DataBaseAccess selectPluSpecialCode:[NSString stringWithFormat:@"select * from PluSpecialCode where pssCode = '%@' order by cast(pscCode as int) asc", temp.pssCode]];
    }
}

/////////////初始化菜本////////////////
-(void) initPluBook{
    if (self.pluBooks != nil && [self.pluBooks count]>0) {
        [self.pluBooks removeAllObjects];
    }
    self.pluBooks = [DataBaseAccess selectPluBook:@"select * from PluBook where pbStatus='1' order by cast(pbSeq as int) asc"];
}

/////////////初始化本地主单数组////////////////
-(void) initMyOrderMenus:(NSString *)sql{
    if (self.myOrderMenus != nil && [self.myOrderMenus count]>0) {
        [self.myOrderMenus removeAllObjects];
    }
    self.myOrderMenus = [DataBaseAccess selectMyOrderMenus:sql];
}
//////////////查询菜品对象///////////////////
-(void) initSearchPlus:(NSString *)sql{
    if (self.searchPlus != nil && [self.searchPlus count]>0) {
        [self.searchPlus removeAllObjects];
    }
    self.searchPlus = [DataBaseAccess selectPluPlu:sql];
}

/////////////初始化当前主单对象////////////
//只有在点击菜本时调用一次！
//而且一定是没有保存的单子
-(void) initCurrentOrderMenu:(NSInteger)pbIndex{
    if (pbIndex >= [self.pluBooks count]) {
        self.bookIndex = -1;
        return;
    }
    self.bookIndex = pbIndex;
    PluBook *tempBook = (PluBook *)[self.pluBooks objectAtIndex:pbIndex];
    self.currentOrderMenu = [DataBaseAccess getOrderMenuByPbCode:tempBook.pbCode];
}
//只有在选中本地主单号时调用，初始化当前主单对象
-(void) getCurrentOrderMenu:(NSString *)momLocalMenu{
    self.currentOrderMenu = [DataBaseAccess getOrderMenuByMomLocalMenu:momLocalMenu];
}

/////////////加载大类/////////////////
//点击菜本时调用
-(void) initPluMenuByBookIndex:(NSInteger)pbIndex{    
    if (pbIndex >= [self.pluBooks count]) {
        self.bookIndex = -1;
        return;
    }
    self.bookIndex = pbIndex;//设置当前菜本序号
    PluBook *tempBook = (PluBook *)[self.pluBooks objectAtIndex:pbIndex];
    [self initPluMenu:tempBook.pmCode];//注意传入的参数(12#03#05#)
}
/////////////加载大类/////////////////
//点击未发送时调用
-(void) initPluMenuByBookCode:(NSString *)pbCode{    
    NSString * pmCodes;
    for (int i=0; i<self.pluBooks.count; i++) {
        PluBook *tempBook = (PluBook *)[self.pluBooks objectAtIndex:i];
        if ([pbCode isEqualToString:tempBook.pbCode]) {
            pmCodes = tempBook.pmCode;
            self.bookIndex = i;//设置当前菜本序号
            break;
        }
    }
    [self initPluMenu:pmCodes];//注意传入的参数(12#03#05#)
}
-(void) initPluMenu:(NSString *)pmCodes{//注意传入的参数(12#03#05#)
    if (self.pluMenus != nil && [self.pluMenus count]>0) {
        [self.pluMenus removeAllObjects];
    }
    
    if (pmCodes == nil) {
        pmCodes = @"";
    }
    //去掉最后的#
    if ([pmCodes isContainString:@"#"] && [[pmCodes substringWithRange:NSMakeRange([pmCodes length]-1, 1)] isEqualToString:@"#"]) {
        pmCodes = [pmCodes substringToIndex:[pmCodes length]-1];
    }
    
    self.currentPmCodes = [NSString stringWithFormat:@"('%@')",[pmCodes replaceFrom:@"#" To:@"','"]];
    NSString *sql = [NSString stringWithFormat:@"select * from PluMenu where pmStatus='1' and pmCode in %@ order by cast(pmSeq as int) asc", self.currentPmCodes];
    self.pluMenus = [DataBaseAccess selectPluMenu:sql];
}



/////////////加载小类////////////////
//只有在点击大类时调用！
-(void) initPluSortByMenuIndex:(NSInteger)pmIndex {
    if (pmIndex>=[self.pluMenus count]) {
        return;
    }
    self.menuIndex = pmIndex;
    self.sortIndex = -1;//默认不显示小类
    PluMenu *tempMenu = (PluMenu *)[self.pluMenus objectAtIndex:pmIndex];
    [self initPluSort:tempMenu.pmCode];
}
-(void) initPluSort:(NSString *)pmCode {
    if (self.pluSorts != nil && [self.pluSorts count]>0) {
        [self.pluSorts removeAllObjects];
    }
    if (pmCode == nil) {
        pmCode = @"";
    }
    NSString *sql = [NSString stringWithFormat:@"select * from PluSort where psStatus='1' and pmCode='%@' order by cast(psSeq as int) asc",pmCode];
    self.pluSorts = [DataBaseAccess selectPluSort:sql];
}


////////////////////初始化推荐菜数组///////////////////
//只有在推荐菜界面初始化时调用！
-(void) initCommendPluPlus{
    if (self.commendPluPlus != nil && [self.commendPluPlus count]>0) {
        [self.commendPluPlus removeAllObjects];
    }
    NSString *sql = [NSString stringWithFormat:@"select * from PluPlu where ppStatus='1' and ppRecommended = '1' and pmCode in %@ order by cast(ppSeqCommend as int) asc", self.currentPmCodes];
    self.commendPluPlus = [DataBaseAccess selectPluPlu:sql];
}
//////////////////初始化大类下的菜品数组////////////////
//只有在点击大类时调用！
-(void) initPlusByMenuIndex:(NSInteger)pmIndex{
    if (pmIndex >= [self.pluMenus count]) {
        return;
    }
    if (self.orderingPluPlus != nil && [self.orderingPluPlus count]>0) {
        [self.orderingPluPlus removeAllObjects];
    }
    self.menuIndex = pmIndex;
    self.sortIndex = -1;//默认不显示小类
    PluMenu *tempMenu = (PluMenu *)[self.pluMenus objectAtIndex:pmIndex];
    NSString *sql = [NSString stringWithFormat:@"select * from PluPlu where ppStatus='1' and pmCode = '%@' order by cast(ppSeqNormal as int) asc", tempMenu.pmCode];
    self.orderingPluPlus = [DataBaseAccess selectPluPlu:sql];
}
//////////////////初始化小类下的菜品数组////////////////
//只有在点击小类时调用
-(void) initPlusBySortIndex:(NSInteger)psIndex{
    if (psIndex >= [self.pluSorts count]) {
        return;
    }
    if (self.orderingPluPlus != nil && [self.orderingPluPlus count]>0) {
        [self.orderingPluPlus removeAllObjects];
    }
    self.sortIndex = psIndex;//只有在点击小类时存储该变量
    PluSort *tempSort = (PluSort *)[self.pluSorts objectAtIndex:psIndex];
    NSString *sql = [NSString stringWithFormat:@"select * from PluPlu where ppStatus='1' and psCode = '%@' order by cast(ppSeqNormal as int) asc", tempSort.psCode];
    self.orderingPluPlus = [DataBaseAccess selectPluPlu:sql];
}

//////////////////初始化我的点单明细菜品数组////////////////
//只有在显示或刷新九宫格我的点单界面时调用
-(void) initMyOrderDishes{
    if (self.myOrderDishes != nil && [self.myOrderDishes count]>0) {
        [self.myOrderDishes removeAllObjects];
    }
    NSString *sql = [NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu = '%@' order by modTime asc", self.currentOrderMenu.momLocalMenu]; 
    self.myOrderDishes = [DataBaseAccess selectMyOrderDishes:sql];
}
//初始化已点的套餐明细(只有在我的点单列表模式界面点击套餐按钮时+送单时调用)
//送单调用时传递nil参数
-(void) initMyOrderPluPkgs:(NSString *)ppCode{
    if (self.myOrderPluPkgs != nil && [self.myOrderPluPkgs count]>0) {
        [self.myOrderPluPkgs removeAllObjects];
    }
    NSString *sql = [NSString stringWithFormat:@"select * from MyOrderPluPkgs where momLocalMenu = '%@' order by ppCode asc", self.currentOrderMenu.momLocalMenu]; 
    if (ppCode != nil && ppCode.length >1) {
        sql = [NSString stringWithFormat:@"select * from MyOrderPluPkgs where momLocalMenu = '%@' and ppCode = '%@' order by ppCode asc", self.currentOrderMenu.momLocalMenu, ppCode]; 
    }
    self.myOrderPluPkgs = [DataBaseAccess selectMyOrderPluPkgs:sql];
}
//初始化我的点单分类数组
-(void) initMyOrderSortedDishes{
    NSString *sql=nil;
    //初始化分类显示数据
    if (self.myOrderSortedType != nil && [self.myOrderSortedType count]>0) {
        [self.myOrderSortedType removeAllObjects];
    }
    if (self.myOrderSortedDishes != nil && [self.myOrderSortedDishes count]>0) {
        [self.myOrderSortedDishes removeAllObjects];
    }
    if (isSortedByMenu) {//按大类分
        sql = [NSString stringWithFormat:@"select distinct b.* from MyOrderDishes a,pluMenu b where a.pmCode=b.pmCode and a.momLocalMenu = '%@' order by a.modTime asc", self.currentOrderMenu.momLocalMenu ];
        self.myOrderSortedType = [DataBaseAccess selectPluMenu:sql];
        
        NSMutableDictionary * tempDishes=[[NSMutableDictionary new] autorelease];
        for (int i=0; i<self.myOrderSortedType.count; i++) {
            PluMenu *tempPluMenu = (PluMenu *)[self.myOrderSortedType objectAtIndex:i];
            sql = [NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu = '%@' and pmCode='%@' order by modTime asc", self.currentOrderMenu.momLocalMenu,tempPluMenu.pmCode]; 
            NSMutableArray *tempArray = [DataBaseAccess selectMyOrderDishes:sql];
            [tempDishes setObject:tempArray forKey:[NSString stringWithFormat:@"%@[%@]",JsonField(tempPluMenu.pmDescripts),tempPluMenu.pmCode]];
        }
        self.myOrderSortedDishes = tempDishes;
    }else {//按小类分
        sql = [NSString stringWithFormat:@"select distinct b.* from MyOrderDishes a,pluSort b where a.psCode=b.psCode and a.momLocalMenu = '%@' order by a.modTime asc", self.currentOrderMenu.momLocalMenu ];
        self.myOrderSortedType = [DataBaseAccess selectPluSort:sql];
        
        NSMutableDictionary * tempDishes=[[NSMutableDictionary new] autorelease];
        for (int i=0; i<self.myOrderSortedType.count; i++) {
            PluSort *tempPluSort = (PluSort *)[self.myOrderSortedType objectAtIndex:i];
            sql = [NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu = '%@' and psCode='%@' order by modTime asc", self.currentOrderMenu.momLocalMenu,tempPluSort.psCode]; 
            NSMutableArray *tempArray = [DataBaseAccess selectMyOrderDishes:sql];
            [tempDishes setObject:tempArray forKey:[NSString stringWithFormat:@"%@[%@]",JsonField(tempPluSort.psDescripts),tempPluSort.psCode]];
        }
        self.myOrderSortedDishes = tempDishes;
    }
}

-(void) initRemoteMenus{
    if (self.remoteMenus != nil && [self.remoteMenus count]>0) {
        [self.remoteMenus removeAllObjects];
    }
    
    NSString *sql = [NSString stringWithFormat:@"select * from RemoteMenus order by rmMenu desc"]; 
    self.remoteMenus = [DataBaseAccess selectRemoteMenus:sql];
}

-(void) initRemoteDishes{//只能按小类划分
    //初始化分类显示数据
    if (self.remoteSortedMenus != nil && [self.remoteSortedMenus count]>0) {
        [self.remoteSortedMenus removeAllObjects];
    }
    if (self.remoteSortedDishes != nil && [self.remoteSortedDishes count]>0) {
        [self.remoteSortedDishes removeAllObjects];
    }
    
    NSString *sql = [NSString stringWithFormat:@"select distinct b.* from RemoteDishes a,pluSort b where a.rdSort=b.psCode order by a.rdInumber asc"];
    self.remoteSortedMenus = [DataBaseAccess selectPluSort:sql];
    
    NSMutableDictionary * tempDishes=[[NSMutableDictionary new] autorelease];
    NSString * tempSorts=@"(";
    for (int i=0; i<self.remoteSortedMenus.count; i++) {
        PluSort *tempPluSort = (PluSort *)[self.remoteSortedMenus objectAtIndex:i];
        tempSorts = [tempSorts stringByAppendingFormat:@"'%@'",tempPluSort.psCode];
        sql = [NSString stringWithFormat:@"select * from RemoteDishes where rdSort='%@' order by rdInumber asc", tempPluSort.psCode]; 
        NSMutableArray *tempArray = [DataBaseAccess selectRemoteDishes:sql];
        [tempDishes setObject:tempArray forKey:[NSString stringWithFormat:@"%@[%@]",JsonField(tempPluSort.psDescripts),tempPluSort.psCode]];
        if (i < self.remoteSortedMenus.count-1) {
            tempSorts = [tempSorts stringByAppendingString:@","];
        }
    }
    tempSorts = [tempSorts stringByAppendingString:@")"];
    //////////////////////本地数据库没有小类的处理////////////////////
    sql = [NSString stringWithFormat:@"select * from RemoteDishes where rdSort not in %@ order by rdInumber asc",tempSorts];
    NSMutableArray *tempArray1 = [DataBaseAccess selectRemoteDishes:sql];
    [tempDishes setObject:tempArray1 forKey:@"其它"];
    ///////////////////////end//////////////////////////////////
    self.remoteSortedDishes = tempDishes;
}

-(void) initOtherImages:(NSString *)imageType{
    if (self.otherImages != nil && [self.otherImages count]>0) {
        [self.otherImages removeAllObjects];
    }
    self.otherImageIndex = 0;
    NSString *sql = [NSString stringWithFormat:@"select * from OtherImage where imageType='%@' order by imageName asc", imageType];
    self.otherImages = [DataBaseAccess selectOtherImages:sql];
}

////////////对象初始化///////
-(id) init{
	self = [super init];
	if (self) {
        isAnimating = NO;
        isViewRemoveFromLeft = YES;
        isCommendView = YES;
        isPopOutFullScreen = NO;
        fullScreenType = 0;   
        supportLangs = @"zh-Hans#en";//支持的多语言 最多七种：zh-Hans#zh-Hant#en#ja#ko#ru#fr
        langsArray = [NSMutableArray new];
	}
	return self;
}

//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    [musicPlayer release];
    musicPlayer = nil;
    [hotelName release];
    hotelName = nil;
    [ipadUdid release];
    ipadUdid = nil;
    [downloadUrl release];
    downloadUrl = nil;
    [supportLangs release];
    supportLangs = nil;
    [langsArray release];
    langsArray = nil;
    [language release];
    language = nil;
    [translate release];
    translate = nil;
    [popController release];
    popController = nil;
    [currentOrderMenu release];
    currentOrderMenu = nil;
    [pluBooks release];
    pluBooks = nil;
    [currentPmCodes release];
    currentPmCodes = nil;
    [pluMenus release];
    pluMenus = nil;
    [pluSorts release];
    pluSorts = nil;
    [searchPlus release];
    searchPlus = nil;
    [commendPluPlus release];
    commendPluPlus = nil;
    [orderingPluPlus release];
    orderingPluPlus = nil;
    [myOrderMenus release];
    myOrderMenus = nil;
    [myOrderDishes release];
    myOrderDishes = nil;
    [myOrderPluPkgs release];
    myOrderPluPkgs = nil;
    [myOrderSortedType release];
    myOrderSortedType = nil;
    [myOrderSortedDishes release];
    myOrderSortedDishes = nil;
    [remoteMenus release];
    remoteMenus = nil;
    [remoteSortedMenus release];
    remoteSortedMenus = nil;
    [remoteSortedDishes release];
    remoteSortedDishes = nil;
    
    [pluTables release];
    pluTables = nil;
    [pluEmpnos release];
    pluEmpnos = nil;
    [pluSpecialSorts release];
    pluSpecialSorts = nil;
    [pluSpecialCodes release];
    pluSpecialCodes = nil;
    
    [otherImages release];
    otherImages = nil;
    [super dealloc];
}
@end
